package com.code.lightning.generator;

import com.code.lightning.config.BasicGeneratorConfig;
import com.code.lightning.utils.DateUtil;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 模板库
 * @author LGX_TvT
 * @date 2021-01-16 10:26
 */
public class TemplateKeys {

    /** yyyy-MM-dd */
    public final static String DATE_FORMAT_1 = "dateFormat1";
    /** yyyy-MM-dd HH:mm:ss */
    public final static String DATE_FORMAT_2 = "dateFormat2";
    /**  yyyy年MM月dd日 */
    public final static String DATE_FORMAT_3 = "dateFormat3";
    /**  yyyy年MM月dd日 HH时mm分ss秒 */
    public final static String DATE_FORMAT_4 = "dateFormat4";
    /**  yyyyMMdd */
    public final static String DATE_FORMAT_5 = "dateFormat5";
    /** 作者 @author */
    public final static String AUTHOR = "author";
    /** VERSION */
    public final static String VERSION = "version";

    /**
     * 获取默认Key
     * @param config
     * @return
     */
    public Map<String, Object> getDefaultKeys(BasicGeneratorConfig config) {
        Map<String, Object> keyMap = new HashMap<>();
        keyMap.put(TemplateKeys.DATE_FORMAT_1, DateUtil.format(new Date(), DateUtil.format2));
        keyMap.put(TemplateKeys.DATE_FORMAT_2, DateUtil.format(new Date(), DateUtil.format4));
        keyMap.put(TemplateKeys.DATE_FORMAT_3, DateUtil.format(new Date(), DateUtil.format3));
        keyMap.put(TemplateKeys.DATE_FORMAT_4, DateUtil.format(new Date(), DateUtil.format5));
        keyMap.put(TemplateKeys.DATE_FORMAT_5, DateUtil.format(new Date(), DateUtil.format1));
        keyMap.put(TemplateKeys.AUTHOR, config.getAuthor() == null ? "LightingGenerator" : config.getAuthor());
        keyMap.put(TemplateKeys.VERSION, config.getVersion() == null ? "v1.0.0" : config.getVersion());

        // config里面参数
        Class clazz = config.getClass();
        for (; clazz != null; clazz = clazz.getSuperclass()) {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                try {
                    field.setAccessible(true);
                    keyMap.put(field.getName(), field.get(config));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return keyMap;
    }
}
